﻿using SmartSpace.Core;
using SmartSpace.Workflow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.OA
{
    public class LeaveService : ILeaveService
    {
        private IWorkflowService _workflowService;

        public LeaveService(IWorkflowService workflowService)
        {
            _workflowService = workflowService;
        }

        public ServiceResult Save(LeaveRequest request)
        {
            using (OADbContext context = new OADbContext())
            {
                EntityRepository.Save<LeaveRequest>(request, context);

                return new ServiceResult(EntityRepository.Commit(context));
            }
        }

        public ServiceResult Delete(Guid requestId)
        {
            using (OADbContext context = new OADbContext())
            {
                EntityRepository.Delete<LeaveRequest>(requestId, context);
                return new ServiceResult(EntityRepository.Commit(context));
            }
        }
        
        public SingleResult<LeaveRequest> Get(Guid requestId)
        {
            using (OADbContext context = new OADbContext())
            {
                LeaveRequest request = context.Set<LeaveRequest>().Where(p => p.Id == requestId).SingleOrDefault();
                return new SingleResult<LeaveRequest>(request);
            }
        }

        public PagedResult<LeaveRequest> GetRequests(string creator, int start, int limit)
        {
            using (OADbContext context = new OADbContext())
            {
                List<LeaveRequest> list = context.Set<LeaveRequest>().Where(p => p.Creator == creator)
                    .OrderBy(p => p.CreatedDate)
                    .Skip(start)
                    .Take(limit)
                    .ToList();
                return new PagedResult<LeaveRequest>(start, limit, list.Count, list);
            }
        }

        public PagedResult<LeaveRequest> GetRequests(string creator, string status, int start, int limit)
        {
            using (OADbContext context = new OADbContext())
            {
                List<LeaveRequest> list = context.Set<LeaveRequest>().Where(p => p.Creator == creator && p.StatusCode == status)
                    .OrderBy(p => p.CreatedDate)
                    .Skip(start)
                    .Take(limit)
                    .ToList();
                return new PagedResult<LeaveRequest>(start, limit, list.Count, list);
            }
        }


        public ServiceResult SaveLeaveType(LeaveType type)
        {
            return null;
        }

        public List<LeaveType> GetLeaveTypes()
        {
            return null;
        }

        public LeaveType GetLeaveType(string code)
        {
            return null;
        }
    }
}
